//
//  MenuController.m
//  MoneyIndividual
//
//  Created by Le Anh Tai on 12/24/13.
//  Copyright (c) 2013 Le Anh Tai. All rights reserved.
//

#import "MenuController.h"
#import "MenuMediator.h"
#import "MenuItemCell.h"
#import "Utility.h"

@interface MenuController ()

@end

@implementation MenuController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    ECSlidingViewController *slideController = [MenuMediator shared].slidingController;
    [slideController setAnchorRightRevealAmount:self.view.width/7*6];
    slideController.underLeftWidthLayout = ECFixedRevealWidth;
    
    [self initializeMenuItems];
    
    self.title = @"MONEY INDIVIDUAL";
}

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    
    self.tableView.frame = RECT(0, 0, self.view.width, self.view.height);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initializeMenuItems
{
    _menuItems = [[NSMutableArray alloc] init];
    for (int i = ScreenNameHomeController; i<=MENU_ITEM_COUNT; i++)
    {
        [_menuItems addObject:[self newMenuItemAtRow:i]];
    }
}

- (MenuItemCell*)newMenuItemAtRow:(NSInteger)row
{
    MenuItemCell *cell;
    
    //summary
    if (row == ScreenNameHomeController)
    {
        cell = [[MenuItemCell alloc]initWithNormalImage:[UIImage imageNamed:@"icon_home.png" color:COLOR_BLACK_PURE] selectedImage:[UIImage imageNamed:@"icon_home.png" color:COLOR_WHITE_PURE] reuseIdentifier:nil];
        cell.titleLabel.text = NSLocalizedString(@"TÀI CHÍNH CỦA BẠN", nil);
        cell.descriptionLabel.text = NSLocalizedString(@"Tổng hợp thu, chi, tiền mặt, vốn", nil);
    }
    else if (row == ScreenNameTransactionBookController)
    {
        cell = [[MenuItemCell alloc]initWithNormalImage:[UIImage imageNamed:@"icon_transaction_book.png" color:COLOR_BLACK_PURE] selectedImage:[UIImage imageNamed:@"icon_transaction_book.png" color:COLOR_WHITE_PURE] reuseIdentifier:nil];
        cell.titleLabel.text = NSLocalizedString(@"SỔ THU CHI", nil);
        cell.descriptionLabel.text = NSLocalizedString(@"Lịch sử thu, chi, cho vay, nợ, hụi", nil);
    }
    else if (row == ScreenNameLoanController)
    {
        cell = [[MenuItemCell alloc]initWithNormalImage:[UIImage imageNamed:@"icon_loan.png" color:COLOR_BLACK_PURE] selectedImage:[UIImage imageNamed:@"icon_loan.png" color:COLOR_WHITE_PURE] reuseIdentifier:nil];
        cell.titleLabel.text = NSLocalizedString(@"CHO VAY", nil);
        cell.descriptionLabel.text = NSLocalizedString(@"Quản lí các khoản cho vay", nil);
    }
    else if (row == ScreenNameDebtController)
    {
        cell = [[MenuItemCell alloc]initWithNormalImage:[UIImage imageNamed:@"icon_debt.png" color:COLOR_BLACK_PURE] selectedImage:[UIImage imageNamed:@"icon_debt.png" color:COLOR_WHITE_PURE] reuseIdentifier:nil];
        cell.titleLabel.text = NSLocalizedString(@"NỢ", nil);
        cell.descriptionLabel.text = NSLocalizedString(@"Quản lí các khoản nợ", nil);
    }
    else if (row == ScreenNameHuiSummaryController)
    {
        cell = [[MenuItemCell alloc]initWithNormalImage:[UIImage imageNamed:@"icon_hui.png" color:COLOR_BLACK_PURE] selectedImage:[UIImage imageNamed:@"icon_hui.png" color:COLOR_WHITE_PURE] reuseIdentifier:nil];
        cell.titleLabel.text = NSLocalizedString(@"CHƠI HỤI", nil);
        cell.descriptionLabel.text = NSLocalizedString(@"Quản lí các dây hụi , lãi, lỗ", nil);
    }
    else if (row == ScreenNameChartController)
    {
        cell = [[MenuItemCell alloc]initWithNormalImage:[UIImage imageNamed:@"icon_chart.png" color:COLOR_BLACK_PURE] selectedImage:[UIImage imageNamed:@"icon_chart.png" color:COLOR_WHITE_PURE] reuseIdentifier:nil];
        cell.titleLabel.text = NSLocalizedString(@"BIỂU ĐỒ", nil);
        cell.descriptionLabel.text = NSLocalizedString(@"So sánh thu chi, % chi tiêu", nil);
    }
    else if (row == ScreenNameSettingViewController)
    {
        cell = [[MenuItemCell alloc]initWithNormalImage:[UIImage imageNamed:@"icon_setting.png" color:COLOR_BLACK_PURE] selectedImage:[UIImage imageNamed:@"icon_setting.png" color:COLOR_WHITE_PURE] reuseIdentifier:nil];
        cell.titleLabel.text = NSLocalizedString(@"CÀI ĐẶT", nil);
        cell.descriptionLabel.text = NSLocalizedString(@"Ngân sách, backup, password...", nil);
    }
    
    return cell;
}

- (void)selectMenuItem:(int)menuIdx
{
    if (menuIdx > ScreenNameNone)
    {
        [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:menuIdx - 1 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
        [[MenuMediator shared] openScreen:menuIdx IsSelectMenuItem:NO];
        [[MenuMediator shared].slidingController resetTopView];
    }
}

#pragma mark - UITableView delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return HEI_CELL_MENU;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _menuItems.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MenuItemCell *cell = [Utility getObject:_menuItems atIndex:indexPath.row];        
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self selectMenuItem:indexPath.row + 1];
}
@end
